<template>
    <div class="buildmess">
        <div class="navButton">
            <Button class="navButton_checked" type="primary" @click="modal1 = true" style="width: 100px;">审核</Button >
            <Button  type="success" @click="goLog" style="width: 100px;">查看日志</Button >
        </div>
        <div class="main">
             <buildmanageAdd />
        </div>
        <Modal v-model="modal1" title="请进行审核" footer-hide >
        <div>
            <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="120">
                <FormItem label="审核结果" prop="result">
                    <Select v-model="formValidate.result" placeholder="请选择审核结果">
                        <Option value="1">通过</Option>
                        <Option value="2">不通过</Option>
                    </Select>
                </FormItem>
                <FormItem label="审核建议" prop="desc" v-if="formValidate.result != 1">
                    <Input v-model="formValidate.desc" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder="请输入审核建议"></Input>
                </FormItem>
                <FormItem>
                    <Button type="primary" :disabled="flag" @click="handleSubmit('formValidate')">{{formValidate.result ==1 ? '生成静态页面' : '确定'}}</Button>
                </FormItem>
            </Form>
        </div>
    </Modal>
    <building-log :propertyLogState="propertyLogState" v-if="propertyLogState" :logData="logData" @closePopup="closePopup"/>
    </div>
</template>
 <script>
   import buildmanageAdd from '@pages/operationsCenter/build/detail';
   import buildingLog from "@/components/buildingCheckcenter/buildingLog";
   import {getBuildingCheck, getBuildingHtml,getInitStandingBook} from '@/api/buildingCheckcenter'
   export default {
      components:{
       buildmanageAdd,
       buildingLog
      },
      data(){
         return{
            navTitle:'楼盘详情',
            modal1:false,
            ruleValidate: {
                result: [
                    { required: true, message: '请选择审核结果', trigger: 'blur' }
                ],
                desc: [
                    { required: true, message: '请填写审核建议', trigger: 'blur' },
                    { type: 'string', min: 5, message: '请填写不少于5个字的建议', trigger: 'blur' },
                    { type: 'string', max: 2000, message: '你的建议超出长度了', trigger: 'blur' }
                ]
            },
            formValidate:{
                result:'',
                desc:''
            },
            propertyLogState:false,
            logData:{},
            flag:false
         }
      },
      mounted(){
        this.init() 
      },
      watch:{
        modal1(newValue){
            !newValue && (this.formValidate = { result:'', desc:''})
        },
        $route() {
            console.log(this.$route)
       if(this.$route.name == "firstlevel-checkcenter-buildingImg"){
          // this.navTitle[0].name = this.$route.query.checkType == 2?'二级审核':'一级审核';
          this.$route.query.checkType == 2?document.title = "二级审核-楼盘信息":''
        }
	  }
      },
      methods:{
        init(){
            this.buildingId = this.$route.query.id || '';
        },
        handleSubmit (name) {
          if (this.flag) return
          this.flag = true
            this.$refs[name].validate((valid) => {
                    if (valid) {
                        let {result,desc} = this.formValidate,{checkType, bid, auditId} = this.$route.query;
                        let params = {
                            buildingId:bid || '',
                            auditStatus:checkType == 1?(result==1?'3':"4"):(result==1?'5':"6"),
                            auditFailComment:desc,
                            auditId:auditId
                        }
                        getBuildingCheck(params).then(res => {
                              this.$Message.success('审核成功');
                              
                              setTimeout(() => {
                                this.flag = false
                              }, 5000);
                              this.buildingHtml();
                              let route = checkType == 1?'firstlevelCheckcenter':"secondlevelCheckcenter"
                              if(checkType == 1){
                                this.$router.push({
                                  path:`/operationsCenter/buildingCheckcenter/${route}`,
                                  query:{
                                      tabName:result==1?'1':"2"
                                  }
                                })
                              }
                        }).catch(err=>{this.$Message.error(err);this.flag = false})
                        
                    } 
                })              
        },
        //生成静态页面
        buildingHtml(){
            console.log("开始生成今天页面")
           getBuildingHtml({buildingId:this.$route.query.bid}).then(async res=>{
              this.$Message.success("静态页面生成成功");
              if(this.$route.query.checkType == 2){
                await this.getInitStandingBook()
              }
           })
        },
        //生成静态页面成功后初始化台账
        getInitStandingBook(){
          let userId = this.$tool.getStorage("userMess").userId;
          let params = {
            buildingId:this.$route.query.bid,
            createId:this.$route.query.createId?this.$route.query.createId:userId
          }
          getInitStandingBook(params).then(res=>{
            console.log(res,'初始化楼盘台账')
            let {result} = this.formValidate,{checkType} = this.$route.query;
            this.modal1 = false;
            let route = checkType == 1?'firstlevelCheckcenter':"secondlevelCheckcenter"
            this.$router.push({
                path:`/operationsCenter/buildingCheckcenter/${route}`,
                query:{
                    tabName:result==1?'1':"2"
                }
            })
          }).catch(err=>{
            console.log(err,'初始化台账失败')
          })
        },
        closePopup(){
           this.logData= {};
           this.propertyLogState = false; 
       },
       goLog(){
           this.logData= {
              buildingId:this.buildingId
           };
           this.propertyLogState = true;
        },
      }
   }
  </script>
  <style lang="scss" scoped>
    .buildmess{
        width: 100%;
        height: 100%;
        background-color: white;
        position: relative;
        overflow: hidden;
       padding-bottom: 10px;
    }
   .navButton{
      background-color: white;
      padding: 10px;
      &_checked{
        margin-right: 8px;  
      }
   }
   .main{
     overflow: auto;
     height: 95%;
   }
    
  </style>